<template>
  <div>
    <div>
      <el-form
        ref="form"
        :model="form"
        label-width="100px"
        style="display: flex; justify-content: space-around"
      >
        <h1>查询店铺</h1>
        <el-form-item label="店铺名称">
          <el-input
            placeholder="请输入店铺名称"
            v-model="form.name"
            clearable
            style="width: 200px"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="店铺地址">
          <el-input
            placeholder="请输入店铺地址"
            v-model="form.address"
            style="width: 200px"
            clearable
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch">确认查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="rows" border style="width: 100%">
      <el-table-column prop="name" label="店铺名字" width="180" fixed>
      </el-table-column>
      <el-table-column prop="address" label="店铺地址" width="320">
      </el-table-column>
      <el-table-column prop="tel" label="店铺电话" width="120">
      </el-table-column>
      <el-table-column prop="business" label="营业状态" width="100">
      </el-table-column>
      <el-table-column prop="time" label="营业时间" width="100">
      </el-table-column>
      <el-table-column prop="status" label="店铺状态"> </el-table-column>
      <el-table-column prop="hasDistribution" label="是否可以配送" width="120">
      </el-table-column>
      <el-table-column prop="goods" label="店铺商品" width="80">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleShow(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="店铺操作" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            round
            icon="el-icon-edit"
            type="primary"
            @click="handleEdit(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            round
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="curPage"
      :page-sizes="[5,6,7]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions, mapMutations } =
  createNamespacedHelpers("stores");
export default {
  data() {
    return {
      form: {
        name: "",
        address: "",
      },
      status: true,
    };
  },
  computed: mapState(["rows", "curPage", "maxPage", "eachPage", "total"]),
  mounted() {
    this.getStores();
  },
  methods: {
    ...mapActions(["getStores"]),
    ...mapMutations([
      "setCurPage",
      "setEachPage",
      "deleteStores",
      "searchStores",
    ]),
    handleSizeChange(eachPage) {
      this.setEachPage(eachPage);
    },
    handleCurrentChange(curPage) {
      this.setCurPage(curPage);
    },
    //删除店铺
    handleDelete(rows) {
      this.$confirm("此操作将永久删除该店铺, 是否继续?", "提示", {
        confirmButtonText: "确定删除",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteStores(rows);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getStores();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //修改店铺
    handleEdit(rows) {
      const data = JSON.stringify(rows);
      localStorage.setItem("Editrows", data);
      this.$router.history.push(`/info/editStores`);
    },
    //查看店铺商品
    handleShow(rows) {
       const data = JSON.stringify(rows);
      localStorage.setItem("StoresShopping", data);
      this.$router.history.push(`/info/storesShopping`);
    },
    //查询店铺
    onSearch() {
      this.status =
        this.form.name != "" || this.form.address != "" ? false : true;
      this.searchStores(this.form);
    },
  },
  watch: {
    curPage() {
      this.status ? this.getStores() : this.onSearch();
    },
    eachPage() {
      this.status ? this.getStores() : this.onSearch();
    },
  },
};
</script>
<style scoped></style>
